package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.PageResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

//    @RequiredPermission(name="部门列表",expression = "department:list")

//    @RequiresRoles("ADMIN")
    @RequiresPermissions(value = {"department:list","部门列表"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){
        PageInfo<Department> pageInfo = departmentService.query(qo);
        model.addAttribute("pageResult", pageInfo);

        //判断主体的权限信息
        Subject subject = SecurityUtils.getSubject();
        boolean hasRole = subject.hasRole("HR_MGR");
        System.out.println("用户是否有HR_MGR角色："+hasRole);

        //判断用户是否有某个权限
        boolean permitted = subject.isPermitted("department:delete");
        System.out.println("判断用户是否有department:delete权限:"+permitted);


        return "department/list";
    }

    // 处理删除请求
//    @RequiredPermission(name="部门删除",expression = "department:delete")
    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
//        try {
            if (id != null) {
                departmentService.delete(id);
            }
            return new JsonResult();
//        } catch (Exception e) {
//            e.printStackTrace();
//            return new JsonResult(false,"删除失败");
//        }
    }

    // 处理去保存请求
   /* @RequestMapping("/input")
    public String input(Long id, Model model){
        // /WEB-INF/views/department/input.jsp
        if (id != null) {
            model.addAttribute("department",
                departmentService.get(id));
        }
        return "department/input";
    }*/

    // 处理保存请求
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department dept) {
        try {
            if (dept.getId() != null) { // 修改
                departmentService.update(dept);
            }else { // 保存
                departmentService.save(dept);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"保存失败");
        }
    }







}
